package com.yueyang.sojourn.server.controller.share;

import com.github.pagehelper.PageInfo;
import com.yueyang.sojourn.server.controller.BaseController;
import com.yueyang.sojourn.server.model.vo.ShareDataVo;
import com.yueyang.sojourn.server.model.vo.UploadResult;
import com.yueyang.sojourn.server.service.share.ShareDataService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Api(tags = "分销数据接口")
@RestController
@RequestMapping("/share/data")
public class ShareDataController extends BaseController {
    @Autowired
    private ShareDataService shareDataService;

    @ApiOperation(value="分销数据列表")
    @ApiImplicitParam(name = "X-Token", paramType = "header", required = true)
    @GetMapping("/list")
    public PageInfo<ShareDataVo> findPage(@ApiParam(value = "时间开始") @RequestParam(required = false) Long startTime,
                                          @ApiParam(value = "时间结束") @RequestParam(required = false) Long endTime,
                                          @ApiParam(value = "每页大小") @RequestParam("pageSize") int pageSize,
                                          @ApiParam(value = "页码") @RequestParam("pageNum") int pageNum) {
        Date startDate = startTime == null ? null : new Date(startTime);
        Date endDate = endTime == null ? null : new Date(endTime);
        return shareDataService.findList(startDate, endDate, pageSize, pageNum);
    }

    @ApiOperation(value="导出分销数据,返回文件名")
    @ApiImplicitParam(name = "X-Token", paramType = "header", required = true)
    @GetMapping("/export")
    public UploadResult export(@ApiParam(value = "时间开始") @RequestParam(required = false) Long startTime,
                               @ApiParam(value = "时间结束") @RequestParam(required = false) Long endTime) {
        Date startDate = startTime == null ? null : new Date(startTime);
        Date endDate = endTime == null ? null : new Date(endTime);
        return this.shareDataService.export(startDate, endDate, getLoginUserId());
    }
}
